<?php 
$burl = $this->baseUrl(); ?>
<div id='advmenusystem_notifications'>   
    <ul> 
        <?php if($this->viewer()->getIdentity() > 0):?>  
            <li style="padding: 6px 17px 0px 2px;">
                <a href="javascript:void(0);" id="inbox" class="status" >
                    <span class="inbox_count" style="display:none"><span id="msg_count"></span></span>
                </a>
                <div class="dropdown_box" id="inbox_updates" >
                        <h3 style="color:#333333; font-size:11px; padding: 4px;"><?php echo $this->translate("Messages") ?>
                        <span style="float:right"><a href="<?php echo $this->baseUrl() ?>/messages/compose"><?php echo $this->translate("Send a New Message") ?></a><!-- | <a href="javascript:void(0);" onclick="refreshBox(1)"><?php echo $this->translate("Refresh") ?></a>--></span><hr style="clear:both"/></h3>
                    <div id="inbox_content">
                    </div>
                </div>
            </li>
            <li style="padding: 6px 17px 0px 2px;">
                <a href="javascript:void(0);" id="friend_box" class="status">
                    <span class="inbox_count" style="display:none"><span id="friend_req_count"></span></span>
                </a>
                <div class="dropdown_box" id="friend_updates">
                        <h3 style="color:#333333; font-size:11px; padding: 4px;"><?php echo $this->translate("Friend Requests") ?>
                        <span style="float:right"><a href="<?php echo $this->baseUrl() ?>/members"><?php echo $this->translate("Find Friends") ?></a><!-- | <a href="javascript:void(0);" onclick="refreshBox(2)"><?php echo $this->translate("Refresh") ?></a> --></span><hr style="clear:both"/></h3>
                    <div id="friend_content">
                    </div>
                </div>
            </li>
            <li style="padding: 6px 22px 0px 2px;">
                <a href="javascript:void(0);" id="notification_box" class="status">
                    <span class="inbox_count" style="display:none"><span id="notification_count"></span></span>
                </a>
                <div class="dropdown_box" id="notification_updates">
                        <h3 style="color:#333333; font-size:11px; padding: 4px;"><?php echo $this->translate("Notifications") ?><span style="float:right"><!--<a href="javascript:void(0);" onclick="refreshBox(3)"><?php echo $this->translate("Refresh") ?></a>--></span><hr style="clear:both"/></h3>
                    <div id="notification_content">
                    </div>
                </div>
            </li>
        <?php endif; ?>
    </ul>
</div>  
<script type="text/javascript">
  if (typeof jQuery != 'undefined') { 
     jQuery.noConflict();
  }
  var notificationUpdater;
  var inbox_position = new Array('-50px -95px', '-75px -95px', '-50px -127px', 0);
  var freq_position = new Array('0px -63px', '-25px -63px', '-25px -127px', 0);
  var notif_position = new Array('-75px -63px', '0px -95px', '-75px -127px', 0);
  var inbox_msg = new Array();
  var freq_msg = new Array();
  var notification_msg = new Array();
  function box_position(box, pos)
  {
      if (box == 1) {
          if (pos != -1) {
              inbox_position[3] = pos;
          }
          return inbox_position[inbox_position[3]];
      } else if (box == 2) {
          if (pos != -1) {
              freq_position[3] = pos;
          }
          return freq_position[freq_position[3]];
      } else if (box == 3) {
          if (pos != -1) {
              notif_position[3] = pos;
          }
          return notif_position[notif_position[3]];
      }
  }
  function hide_all_drop_box(except)
  {
      inbox_position[3] = inbox_position[3] == 2 ? 0 : inbox_position[3];
      freq_position[3]  = freq_position[3] == 2 ? 0 : freq_position[3];
      notif_position[3] = notif_position[3] == 2 ? 0 : notif_position[3];
      
      // reset inbox
      if (except != 1) {
          $('inbox').setStyle('background-position', box_position(1, -1));
          $('inbox').removeClass('active');
          $('inbox_updates').hide();
          inbox_status = false;
          inbox_count_down = 1;
      }
      if (except != 2) {
          // reset friend
          $('friend_box').setStyle('background-position', box_position(2, -1));
          $('friend_box').removeClass('active');
          $('friend_updates').hide();
          friend_status = false;
          friend_count_down = 1;
      }
      if (except != 3) {
            // reset notification
          $('notification_box').setStyle('background-position', box_position(3, -1));
          $('notification_box').removeClass('active');
          $('notification_updates').hide();
          notification_status = false;
          notification_count_down = 1;
      }
  }

  //refresh box
  function refreshBox(box) {
      var img_loading = '<?php echo $this->baseUrl(); ?>/application/modules/Advmenusystem/externals/images/fbloading.gif';
      if (box == 1) {
          // refresh message box
          inbox_count_down = 1;
          $('inbox_content').innerHTML = '<center><img src="'+img_loading+'" border="0" /></center>';
          inbox();
      } else if (box == 2) {
          // refresh friend box
          friend_count_down = 1;
          $('friend_content').innerHTML = '<center><img src="'+img_loading+'" border="0" /></center>';
          freq();
      } else if (box == 3) {
          // refresh notification box
          notification_count_down = 1;
          $('notification_content').innerHTML = '<center><img src="'+img_loading+'" border="0" /></center>';
          notification();
      }
  }

  function doConfirm(rid)
  {
      do_confrim_friend = true;
      var img_loading = '<?php echo $this->baseUrl(); ?>/application/modules/Advmenusystem/externals/images/fbloading.gif';
      $('friend_action_'+rid).innerHTML = '<center><img src="'+img_loading+'" border="0" /></center>';
      new Request.HTML({
           'url'    :    en4.core.baseUrl + 'advmenusystem/index/confirm-friend/',
           'data' : {
                'format' : 'html',
                'resource_id' : rid
            },
            'onComplete' : function(responseTree, responseElements, responseHTML, responseJavaScript) {
                if(responseHTML){ 
                    $('freqs_'+rid).setStyle('background', '#FFF9D7');
                    $('freqs_'+rid).innerHTML = responseHTML;
                }
            }
       }).send();
       return false;
  }

  function doCancel(rid)
  {
      do_confrim_friend = true;
      var img_loading = '<?php echo $this->baseUrl(); ?>/application/modules/Advmenusystem/externals/images/fbloading.gif';
      $('friend_action_'+rid).innerHTML = '<center><img src="'+img_loading+'" border="0" /></center>';
      new Request.HTML({
           'url'    :    en4.core.baseUrl + 'advmenusystem/index/cancel-friend/',
           'data' : {
                'format' : 'html',
                'resource_id' : rid
            },
            'onComplete' : function(responseTree, responseElements, responseHTML, responseJavaScript) {
                if(responseHTML){ 
                    $('freqs_'+rid).setStyle('background', '#FFF9D7');
                    $('freqs_'+rid).innerHTML = responseHTML;
                    setTimeout("$('freqs_"+rid+"').destroy()", 1000);
                }
            }
       }).send();
       return false;
  }

var isLoaded = [0, 0, 0]; // friend request, message, notifcation
var timerNotificationID = 0;
//time to check for notification updates (in seconds)
var updateTimes = <?php echo Engine_Api::_()->getApi('settings','core')->getSetting('core.general.notificationupdate',30000) ?>; 
function getNotificationsTotal()
{
    var notif = new Request.JSON({
           url    :    en4.core.baseUrl + 'application/lite.php?module=advmenusystem&name=total',
           onSuccess : function(data) {
                if(data != null)
                {
                       if (data.notification > 0) {
                            var notification_count = $('notification_count');
                            notification_count.set('text', data.notification);
                            notification_count.getParent().setStyle('display', 'block');
                            isLoaded[2] = 0;
                       }
                       else
                       {
                           var notification_count = $('notification_count');
                          notification_count.getParent().setStyle('display', 'none');  
                       }
                       if (data.freq > 0) {
                            var friend_req_count = $('friend_req_count');
                            friend_req_count.set('text', data.freq);
                            friend_req_count.getParent().setStyle('display', 'block');
                            isLoaded[0] = 0;
                       }
                       else
                       {
                           var friend_req_count = $('friend_req_count');
                           friend_req_count.getParent().setStyle('display', 'none'); 
                       }
                       if (data.msg > 0) {
                            var msg_count = $('msg_count');
                            msg_count.set('text', data.msg);
                            msg_count.getParent().setStyle('display', 'block');
                            isLoaded[1] = 0;
                       }
                       else
                       {
                            var msg_count = $('msg_count');
                             msg_count.getParent().setStyle('display', 'none');
                       }
                }              
           }
    }).get();
    <?php if($this->viewer()->getIdentity() > 0): ?>
    if(updateTimes > 10000){
        timerNotificationID = setTimeout('getNotificationsTotal()', updateTimes);
    }
    <?php endif; ?>
}
function adjustAdminMenu()
{
    var x = $(window).getSize().x;
    var l = 0;
    switch (x) {
        case 1680:
            l = 527;
            break;
        default: 
            // 1366 x 768
            l = 509;
            break;
    }
    if (l > 0) 
    {
    }
}
function adjustMiddleBorder()
{
    var l  = $$('.layout_left').getLast();
    var ly = l.getSize().y;
    var r  = $$('.layout_right').getLast();
    var ry = r.getSize().y;
    var m  = $$('.layout_middle').getLast();
    var my = m.getSize().y;
    m.setStyle('min-height', (ly > ry ? ly : ry) + 'px');
}
// DOM READY
window.addEvent('domready', function(){
    // Load notification, friend request, message in total
    en4.core.runonce.add(function(){
        getNotificationsTotal();
        adjustAdminMenu();
        adjustMiddleBorder();
    });
});
  function inbox() {
       new Request.HTML({
           'url'    :    en4.core.baseUrl + 'advmenusystem/index/message',
           'data' : {
                'format' : 'html',
                'page' : 1
            },
            'onComplete' : function(responseTree, responseElements, responseHTML, responseJavaScript) {
                        if(responseHTML){
                              $('inbox_content').innerHTML = responseHTML;
                              if (inbox_msg.length) {
                                  $('inbox').setStyle('background-position', box_position(1, 1));
                                  $('inbox_content').getChildren('ul').getChildren('li').each(function(el){
                                      el.addEvent('click', function(){inbox_count_down = 1;});
                                  });
                              }
                              <?php if(Engine_Api::_()->getApi('settings', 'core')->getSetting('avdmenusystem.makeall', 0) > 0): ?>
                                inboxComplete();
                            <?php endif; ?>
                              
                        }
            }
       }).send();
   }
   //inbox();

   function freq() {
       new Request.HTML({
           'url'    :    en4.core.baseUrl + 'advmenusystem/index/friend',
           'data' : {
                'format' : 'html',
                'page' : 1
            },
            'onComplete' : function(responseTree, responseElements, responseHTML, responseJavaScript) {
                if(responseHTML){ 
                    $('friend_content').innerHTML = responseHTML;
                    if (freq_msg.length) {
                        $('friend_box').setStyle('background-position', box_position(2, 1));
                        $('friend_content').getChildren('ul').getChildren('li').each(function(el){
                           el.addEvent('click', function(){friend_count_down = 1;});
                        });
                    }
                    <?php if(Engine_Api::_()->getApi('settings', 'core')->getSetting('avdmenusystem.makeall', 0) > 0): ?>
                        freqComplete();
                    <?php endif; ?>
                }
            }
       }).send();
   }
   //freq();

   function notification() {
       new Request.HTML({
           'url'    :    en4.core.baseUrl + 'advmenusystem/index/notification',
           'data' : {
                'format' : 'html',
                'page' : 1
            },
            'onComplete' : function(responseTree, responseElements, responseHTML, responseJavaScript) {
                if(responseHTML){ 
                    $('notification_content').innerHTML = responseHTML;        
                    if (notification_msg.length) {
                        $('notification_box').setStyle('background-position', box_position(3, 1));            
                        $('notification_content').getChildren('ul').getChildren('li').each(function(el){
                           el.addEvent('click', function(){notification_count_down = 1;});
                        });
                    }
                    <?php if(Engine_Api::_()->getApi('settings', 'core')->getSetting('avdmenusystem.makeall', 0) > 0): ?>
                        notificationComplete();
                    <?php endif; ?>
                   
                }
            }
       }).send();
   }
   //notification();

   function inboxComplete()
   {
       // Set all message as read
          if (inbox_msg.length && isLoaded[1] == 1) {
              new Request.HTML({
                               'url'    :    en4.core.baseUrl + 'advmenusystem/index/message/',
                               'data' : {
                                    'format' : 'html',
                                    'page' : 1,
                                    'do'    : 'markread',
                                    'nids' : JSON.encode(inbox_msg)
                                },
                                'onComplete' : function() {
                                    $('msg_count').getParent().setStyle('display', 'none');
                                }
                      }).send();
              inbox_msg = new Array();
          }
   }
   function freqComplete()
   {
       if (freq_msg.length && isLoaded[0] == 1) {
              new Request.HTML({
                               'url'    :    en4.core.baseUrl + 'advmenusystem/index/friend/',
                               'data' : {
                                    'format' : 'html',
                                    'page'   : 1,
                                    'do'     : 'markread',
                                    'nids'   : JSON.encode(freq_msg)
                                },
                                'onComplete' : function() {
                                    $('friend_req_count').getParent().setStyle('display', 'none');
                                }
                      }).send();
              freq_msg = new Array();
          }
   }
   function notificationComplete()
   {
          if (notification_msg.length && isLoaded[2] == 1) {
              new Request.HTML({
                               'url'    :    en4.core.baseUrl + 'advmenusystem/index/notification/',
                               'data' : {
                                    'format' : 'html',
                                    'page'   : 1,
                                    'do'     : 'markread',
                                    'nids'   : JSON.encode(notification_msg)
                                },
                                'onComplete' : function() {
                                    $('notification_count').getParent().setStyle('display', 'none');
                                }
                      }).send();
              notification_msg = new Array();
          }
   }
   // read message : set message as read
   var readMessage = function(obj, box, nid) {
        inbox_count_down = 1;
        if (nid) {
            if (box == 1) {
                // in message box
                new Request.HTML({
                       'url'    :    en4.core.baseUrl + 'advmenusystem/index/message/',
                       'data' : {
                            'format' : 'html',
                            'page' : 1,
                            'do'    : 'markread',
                            'nid': nid
                        },
                        'onComplete' : function() {
                            refreshBox(box);
                        }
               }).send();
                   
                
                var total_msg = parseInt($('msg_count').get('text'));            
                $('msg_count').set('text', (--total_msg));    
                if (!total_msg) {
                    $('msg_count').getParent().setStyle('display', 'none');
                    $('inbox_content').innerHTML = '<?php echo $this->translate('No new messages')?>';
                }        
            } else if (box == 2) {
                // in friend request box
                
                new Request.HTML({
                       'url'    :    en4.core.baseUrl + 'advmenusystem/index/friend/',
                       'data' : {
                            'format' : 'html',
                            'page' : 1,
                            'do'    : 'markread',
                            'nid': nid
                        },
                        'onComplete' : function() {
                            refreshBox(box);
                        }
               }).send();                   
                
                var total_msg = parseInt($('friend_req_count').get('text'));            
                $('friend_req_count').set('text', (--total_msg));    
                if (!total_msg) {
                    $('friend_req_count').getParent().setStyle('display', 'none');
                    $('friend_content').innerHTML = '<?php echo $this->translate('No new friend request')?>';
                }
            } else if (box == 3) {
                // in notification box
                
                new Request.HTML({
                       'url'    :    en4.core.baseUrl + 'advmenusystem/index/notification/',
                       'data' : {
                            'format' : 'html',
                            'page' : 1,
                            'do'    : 'markread',
                            'nid': nid
                        },
                        'onComplete' : function() {
                            refreshBox(box);
                        }
               }).send();                   
                
                var total_msg = parseInt($('notification_count').get('text'));            
                $('notification_count').set('text', (--total_msg));    
                if (!total_msg) {
                    $('notification_count').getParent().setStyle('display', 'none');
                    $('notification_content').innerHTML = '<?php echo $this->translate('No new notifications')?>';
                }
            }
            
            // delete this row in html
            //$(obj).getParent().getParent().getParent().getParent().destroy();
        }
    }
    
  en4.core.runonce.add(function(){
    if($('notifications_markread_link')){
      $('notifications_markread_link').addEvent('click', function() {
        //$('notifications_markread').setStyle('display', 'none'); 
          en4.activity.hideNotifications('');
      });
    }
    
  // Show Inbox Message
  var inbox_count_down = 1;
  var inbox_status = false; // false -> not shown, true -> shown
  $('inbox').addEvent('click', function() {
      hide_all_drop_box(1);
      if (inbox_status) inbox_count_down = 1; 
      if (!inbox_status) {
          // show
          $(this).setStyle('background-position', box_position(1, 2));
          $(this).addClass('active');
          $('inbox_updates').setStyle('display', 'block');
      } else {
          // hide
          $(this).setStyle('background-position', box_position(1, 0));
          $(this).removeClass('active');
          $('inbox_updates').setStyle('display', 'none');
      }
      inbox_status = inbox_status ? false : true;
      if (isLoaded[1] == 0) {
          refreshBox(1);
          isLoaded[1] = 1;
      }
  });
  // Friend box
  var friend_count_down = 1;
  var friend_status = false;
  $('friend_box').addEvent('click', function(){
      hide_all_drop_box(2);
      if (friend_status) friend_count_down = 1;
      if (!friend_status) {
          $(this).setStyle('background-position', box_position(2, 2));
          $(this).addClass('active');
          $('friend_updates').setStyle('display', 'block');
      } else {
          $(this).setStyle('background-position', box_position(2, 0));
          $(this).removeClass('active');
          $('friend_updates').setStyle('display', 'none');
      }
      friend_status = friend_status ? false : true; 

      // Set all message as read
      if (isLoaded[0] == 0) {
          refreshBox(2);
          isLoaded[0] = 1;         
      }
  });
  //Notification box
  var notification_count_down = 1;
  var notification_status = false;
  $('notification_box').addEvent('click', function(){
      hide_all_drop_box(3);
      if (notification_status) notification_count_down = 1;
      if (!notification_status) {
          // active
          $(this).setStyle('background-position', box_position(3, 2));
          $(this).addClass('active');
          $('notification_updates').setStyle('display', 'block');
      } else {
          $(this).setStyle('background-position', box_position(3, 0));
          $(this).removeClass('active');
          $('notification_updates').setStyle('display', 'none');
      }
      notification_status = notification_status ? false : true;

      if (isLoaded[2] == 0) {
          refreshBox(3);
          isLoaded[2] = 1;
      }
  });
  do_confrim_friend = false;
  $(document).addEvent('click', function() {
        if (inbox_status && inbox_count_down <= 0) {
            $('inbox').setStyle('background-position', box_position(1, 0));
            $('inbox').removeClass('active');
            $('inbox_updates').setStyle('display', 'none');
                inbox_status = false;            
                inbox_count_down = 1;
        } else if (inbox_status) {
            inbox_count_down = (inbox_count_down <= 0) ? 0 : --inbox_count_down;
        }         
        
        if (friend_status && friend_count_down <= 0) {
            if (do_confrim_friend) {do_confrim_friend = false; return false;}
            $('friend_box').setStyle('background-position', box_position(2, 0));
            $('friend_box').removeClass('active');
            $('friend_updates').setStyle('display', 'none');
            friend_status = false;            
            friend_count_down = 1;
        } else if (friend_status) {
            friend_count_down = (friend_count_down <= 0) ? 0 : --friend_count_down;
        } 
        if (notification_status && notification_count_down <= 0) {
            $('notification_box').setStyle('background-position', box_position(3, 0));
            $('notification_box').removeClass('active');
            $('notification_updates').setStyle('display', 'none');
            notification_status = false;            
            notification_count_down = 1;
        } else if (notification_status) {
            notification_count_down = (notification_count_down <= 0) ? 0 : --notification_count_down;
        }
   });
  
  });

  var toggleUpdatesPulldown = function(event, element, user_id) {
    if( element.className=='updates_pulldown' ) {
      element.className= 'updates_pulldown_active';
      //showNotifications();
    } else {
      element.className='updates_pulldown';
    }
  }
</script>
 
 <script type='text/javascript'>
 
  var toggleAccountPulldown = function(event, element, user_id) {
    var a = $('core_menu_mini_menu_account').getChildren()[0].getChildren('a')[0];
    if( element.className=='account_pulldown' ) {
      element.className= 'account_pulldown_active';
      //showNotifications();      
      a.set('styles', {
          'background': '#FFF',
          'color': '#000',
          'padding': '4px 7px 10px 7px'
      });
      a.getChildren().set('src', 'application/modules/Advmenusystem/externals/images/arrow2_.png');
      $('core_menu_mini_menu_account').set('styles', {
          'background': '#FFF',
          'margin-top': '0px !important'
      });
    } else {
      element.className='account_pulldown';
      a.set('styles', {
          'background': 'none',
          'color': '#FFF',
          'padding': '7px'
      });
      if (LongDTBrowserDetect.browser == 'Firefox') {
          a.setStyle('padding', '4px 7px 10px 7px');
      }
      a.getChildren().set('src', 'application/modules/Advmenusystem/externals/images/arrow2.png');
      $('core_menu_mini_menu_account').set('styles', {
          'background': 'none',
          'margin-top': '-3px !important'
      });
    }
  }

</script>
<script type="text/javascript">
  window.addEvent('domready', function() {
    $$('.downservices > .menu_advnotification_mini').each(
     function(e,i){
       if(e.setAttribute){
         e.setAttribute('href', 'javascript:void(0);');
         e.setAttribute('target', '');
       }
     })
  });
</script>
<script type="text/javascript">
var firefox = false;
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
 if (ffversion>=1)
 {
     firefox = true;
 } 
}

function isMouseLeaveOrEnter(e, handler)
{    
    if (e.type != 'mouseout' && e.type != 'mouseover') return false;
    var reltg = e.relatedTarget ? e.relatedTarget :
    e.type == 'mouseout' ? e.toElement : e.fromElement;
    while (reltg && reltg != handler) reltg = reltg.parentNode;
    return (reltg != handler);
}
</script>
